/*
题目链接：https://leetcode.cn/problems/maximum-number-of-operations-with-the-same-score-ii/description/
	刘沛民	2024-12-3
*/

class Solution {
private:
    int res = 1;
public:
    void dfs(vector<int> array, int l, int r, int sum, int ans){
        if(ans+1<res) return;
        if(l>=r||!((array[l]+array[l+1]==sum)||(array[r]+array[r-1]==sum)||(array[l]+array[r]==sum))){
            res = max(res, ans);
            return;
        }
        int a = 1,b = 1,c = 1;
        if(array[l]+array[l+1]==sum) dfs(array, l+2, r, sum, ans+1);
        if(array[r]+array[r-1]==sum) dfs(array, l, r-2, sum, ans+1);
        if(array[l]+array[r]==sum) dfs(array, l+1, r-1, sum, ans+1);
    }
    
    int maxOperations(vector<int>& nums) {
        
        dfs(nums, 2, nums.size()-1, nums[0]+nums[1], 1);
        dfs(nums, 0, nums.size()-3, nums[nums.size()-2]+nums[nums.size()-1], 1);
        dfs(nums, 1, nums.size()-2, nums[0]+nums[nums.size()-1], 1);

        return res;
        
    }
};

